package com.zhongxin.wymovieticket;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class GuideActivity extends Activity {

	private ViewPager viewPager;
	private PagerAdapter pagerAdapter;
	private ImageView bgImageView;
	private ImageView yimageView;
	private Button closeButton;
	private static final String SHAREDPREFERENCES_NAME = "my_pref";
	private static final String KEY_GUIDE_ACTIVITY = "guide_activity";

	private void setGuided() {
		SharedPreferences settings = getSharedPreferences(
				SHAREDPREFERENCES_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(KEY_GUIDE_ACTIVITY, "false");
		editor.commit();
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		viewPager = (ViewPager) findViewById(R.id.viewpager);

		View view1 = LayoutInflater.from(this).inflate(
				R.layout.viewpager_page1, null);
		bgImageView = (ImageView) view1.findViewById(R.id.guideBgpicture);
		bgImageView.setBackgroundResource(R.drawable.tutorail_image1);
		yimageView = (ImageView) view1.findViewById(R.id.guidepicture);
		yimageView.setBackgroundResource(R.drawable.tutorail_icon1);
		View view2 = LayoutInflater.from(this).inflate(
				R.layout.viewpager_page1, null);
		bgImageView = (ImageView) view2.findViewById(R.id.guideBgpicture);
		bgImageView.setBackgroundResource(R.drawable.tutorail_image2);
		yimageView = (ImageView) view2.findViewById(R.id.guidepicture);
		yimageView.setBackgroundResource(R.drawable.tutorail_icon2);
		View view3 = LayoutInflater.from(this).inflate(
				R.layout.viewpager_page3, null);
		bgImageView = (ImageView) view3.findViewById(R.id.guideBgpicture);
		bgImageView.setBackgroundResource(R.drawable.tutorail_image3);
		yimageView = (ImageView) view3.findViewById(R.id.guidepicture);
		yimageView.setBackgroundResource(R.drawable.tutorail_icon3);
		closeButton = (Button) view3.findViewById(R.id.closeguide);
		closeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 设置已经引导
				setGuided();

				// 跳转
				Intent mIntent = new Intent();
				mIntent.setClass(GuideActivity.this, MainActivity.class);
				GuideActivity.this.startActivity(mIntent);
				GuideActivity.this.finish();
			}
		});
		final ArrayList<View> views = new ArrayList<View>();
		views.add(view1);
		views.add(view2);
		views.add(view3);

		pagerAdapter = new PagerAdapter() {

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				// TODO Auto-generated method stub
				return arg0 == arg1;
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return views.size();
			}

			// 特别注意自己加入这两个方法
			public Object instantiateItem(View container, int position) {
				((ViewPager) container).addView(views.get(position));
				return views.get(position);
			}

			public void destroyItem(View container, int position, Object object) {
				((ViewPager) container).removeView(views.get(position));
			}
		};
		viewPager.setAdapter(pagerAdapter);

	}

}
